package au.com.mason.propertyTracker.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import au.com.mason.propertyTracker.dao.SuburbDAO;
import au.com.mason.propertyTracker.domain.Suburb;

@Service
public class SuburbService {
	private SuburbDAO suburbDAO;
	
	@Autowired
	public SuburbService(SuburbDAO suburbDAO) {
		this.suburbDAO = suburbDAO;
	}
	
	public List<Suburb> getSuburbs() {
		return suburbDAO.getSuburbs();
	}	
	
	public void saveSuburb(Suburb suburb) {
		suburbDAO.save(suburb);
	}
	
	public void updateSuburb(Suburb suburb) {
		suburbDAO.update(suburb);
	}
	
	public Suburb getSuburb(int id) {
		return (Suburb) suburbDAO.findById(id);
	}
	
	public Suburb getSuburb(String suburbName, String postCode) {
		Suburb suburb = (Suburb) suburbDAO.getSuburb(suburbName);
		if (suburb == null) {
			suburb = new Suburb();
			suburb.setName(suburbName);
			suburb.setPostCode(postCode);
			saveSuburb(suburb);
		}
		
		return suburb;
	}
}
